<template>
  <a-modal
    :title="title"
    :width="800"
    :dialogStyle="{ top: '30px' }"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :okButtonProps="{ props: { disabled: disableSubmit } }"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-alert message="抢单时要先输入随机码，随机码在详情中可以查看" banner />
        <br />
          <a-form-item   :labelCol="labelCol" :wrapperCol="wrapperCol" label="随机码">
            <a-input  placeholder="请输入随机码"  v-decorator="['code', validatorRules.code]" />
          </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import Vue from 'vue'
  import { httpAction } from '@/api/manage'
  import moment from "moment";
  export default {
    name: "OrderFormModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        orderCode:'',
        disableSubmit:false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          code:{
            rules: [{
              required: true, message: '请输入随机码!'
            },{
              validator: this.comparisonCode,
            }]
          },
        },
        url: {
          add: "/orderForm/orderForm/add",
          edit: "/orderForm/orderForm/grabEdit",
        },
      }
    },
    created () {

    },
    methods: {
      comparisonCode(rule, value, callback){
        if(this.orderCode==value){
          callback();
        }else {
          callback('随机码错误！');
        }
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
         let orderCode=Vue.ls.get('orderCode'+record.id);
         //console.log("orderCode-->",orderCode);
         this.orderCode=orderCode;
      },
      close () {
        this.$emit('ok');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            httpurl+=this.url.edit;
            method = 'put';
            let formData = Object.assign(this.model, values);
            formData.takeOrders=this.$store.getters.userInfo.username;//保存接单人
            formData.orderStatus=0;//修改状态为 0-进行中<用户抢单后>
            formData.grabTime=moment(new Date().getTime()).format('YYYY-MM-DD HH:mm:ss');//抢单时间
            formData.takeManager=this.$store.getters.userInfo.username;
            if(this.$store.getters.userInfo.rolestr=="visitor"){
              formData.takeManager=formData.takeOrders.substr(0,1);
            }
            console.log("formData-->",formData);
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success('抢单成功！');
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style scoped>

</style>